package com.ih.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ih.mapper.AccountMapper;
import com.ih.pojo.Account;
import com.ih.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class AccountImpl implements AccountService {
    @Autowired
    private AccountMapper mapper;
    /**
     * 用户登录
     * 1 根据用户名查询对应的用户列表
     * 2 判断是否为空，如果为空，登录失败，返回false
     * 3,如果账户非空，输入密码进行md5加密，
     * 4，比较数据库的密码，是否相等，
     * 5，相等，登录成功，否则登录失败
     *
     * @param userName
     * @param passWord
     */
    @Override
    public Boolean login(String userName, String passWord) {
        QueryWrapper<Account> qw=new QueryWrapper<>();
        qw.eq("user_name",userName);
        Account account = mapper.selectOne(qw);
        if (account==null){
            return false;
        }
        //得到加密的密码
        String encodePwd = DigestUtil.md5Hex(passWord);
        //加密的密码和数据库进行比对
        if (Objects.equals(encodePwd,account.getUserPwd())){
            return true;
        }
        return false;
    }

    @Override
    public boolean pwdUser(Account account) {
        QueryWrapper<Account> qw=new QueryWrapper<>();
        qw.eq("user_name",account.getUserName());
        int i = mapper.update(account, qw);
        if (i>0){
            return true;
        }else {
            return false;
        }

    }
}
